<template>
  <div class="container" >
	<!--工具栏-->
	<div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
		<el-form :inline="true" :model="filters" :size="size">
			<el-form-item>
				<el-input v-model="filters.label" placeholder="名称"></el-input>
			</el-form-item>
            <el-form-item label="状态" prop="status">
				 <el-select v-model="filters.status" placeholder="请选择" clearable>
            <el-option
              v-for="item in statusOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
			</el-form-item>
			<el-form-item>
				<kt-button :label="$t('action.search')" perms="sys:sysNotice:view" type="primary" @click="findPage(null)"/>
			</el-form-item>
			<el-form-item>
				<kt-button :label="$t('action.add')" perms="sys:sysNotice:add" type="primary" @click="handleAdd" />
			</el-form-item>
		</el-form>
	</div>
	<!--表格内容栏-->
	<kt-table permsEdit="sys:sysNotice:edit" permsDelete="sys:sysNotice:delete"
		:data="pageResult" :columns="columns" :showItemView="showItemView"
		@findPage="findPage" @handleEdit="handleEdit" @handleDelete="handleDelete" @handleLook="handleLook" @handleRelease="handleRelease" @handleOffline ="handleOffline">
	</kt-table>
	<!--新增编辑界面-->
	<el-dialog :title="operation?'新增':'编辑'" width="40%" :visible.sync="editDialogVisible" :close-on-click-modal="false">
		<el-form :model="dataForm" label-width="80px" :rules="dataFormRules" ref="dataForm" :size="size">

			<el-form-item label="公告标题" prop="title"   >
				<el-input v-model="dataForm.title" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="公告内容" prop="content"   >
        <editor v-model="dataForm.content" :min-height="192"/>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button :size="size" @click.native="editDialogVisible = false">{{$t('action.cancel')}}</el-button>
			<el-button :size="size" type="primary" @click.native="submitForm" :loading="editLoading">{{$t('action.submit')}}</el-button>
		</div>
	</el-dialog>
<!-- 查看页面 -->
  	<el-dialog title="查看" width="40%" :visible.sync="viewDialogVisible" :close-on-click-modal="false">

		<div v-html="noticeContent">
		</div>
      		<div slot="footer" class="dialog-footer">
			<el-button :size="size" @click.native="viewDialogVisible = false">确 定</el-button>
		</div>
	</el-dialog>

  </div>
</template>

<script>
import KtTable from "@/views/Core/KtTable"
import KtButton from "@/views/Core/KtButton"
import { format } from "@/utils/datetime"
import Editor from "@/views/Core/Editor"
export default {
	components:{
			KtTable,
			KtButton,
      Editor
	},
	data() {
		return {
			size: 'small',
			filters: {
				label: '',
        status:''
			},
			columns: [
				// {prop:"id", label:"", minWidth:100},
				{prop:"title", label:"公告标题", minWidth:100},
				// {prop:"content", label:"公告内容", minWidth:100},
				{prop:"status", label:"公告状态", minWidth:100,formatter:this.statusFormat},
				{prop:"createBy", label:"创建人", minWidth:100},
				{prop:"createTime", label:"创建时间", minWidth:100},
			],
			pageRequest: { pageNum: 1, pageSize: 8 },
			pageResult: {},

			operation: false, // true:新增, false:编辑
			editDialogVisible: false, // 新增编辑界面是否显示
			editLoading: false,
			dataFormRules: {
				title: [
					{ required: true, message: '请输入名称', trigger: 'blur' }
				]
			},
			// 新增编辑界面数据
			dataForm: {
				id: null,
				title: null,
				content: null,
				status: null,
				createBy: null,
				createTime: null,
				lastUpdateBy: null,
				lastUpdateTime: null,
			},
      noticeContent:'',
      viewDialogVisible:false,
      showItemView: true,
            statusOptions:[
        {id:1,name:"草稿"},
        {id:2,name:"已发布"},
        {id:3,name:"已下线"},

      ]
		}
	},
	methods: {
		// 获取分页数据
		findPage: function (data) {
			if(data !== null) {
				this.pageRequest = data.pageRequest
			}
      this.pageRequest.params = [{name:'name', value:this.filters.label},{name:'status', value:this.filters.status}]
			this.$api.sysNotice.findPage(this.pageRequest).then((res) => {
				this.pageResult = res.data
			}).then(data!=null?data.callback:'')
		},
		// 批量删除
		handleDelete: function (data) {
			this.$api.sysNotice.batchDelete(data.params).then(data!=null?data.callback:'')
		},
		// 显示新增界面
		handleAdd: function () {
			this.editDialogVisible = true
			this.operation = true
			this.dataForm = {
				id: null,
				title: null,
				content: null,
				status: null,
				createBy: null,
				createTime: null,
				lastUpdateBy: null,
				lastUpdateTime: null,
			}
		},
		// 显示编辑界面
		handleEdit: function (params) {
			this.editDialogVisible = true
			this.operation = false
			this.dataForm = Object.assign({}, params.row)
		},
    handleLook: function (params) {
			this.viewDialogVisible = true
			this.noticeContent = params.row.content
		},
                // 发布
		handleRelease: function (data) {
      this.$api.sysNotice.release(data.params).then(data!=null?data.callback:'')
		},
            // 下架
		handleOffline: function (data) {
      this.$api.sysNotice.offline(data.params).then(data!=null?data.callback:'')
		},

		// 编辑
		submitForm: function () {
			this.$refs.dataForm.validate((valid) => {
				if (valid) {
					this.$confirm('确认提交吗？', '提示', {}).then(() => {
						this.editLoading = true
						let params = Object.assign({}, this.dataForm)
						this.$api.sysNotice.save(params).then((res) => {
							if(res.code == 200) {
								this.$message({ message: '操作成功', type: 'success' })
							} else {
								this.$message({message: '操作失败, ' + res.msg, type: 'error'})
							}
							this.editLoading = false
							this.$refs['dataForm'].resetFields()
							this.editDialogVisible = false
							this.findPage(null)
						})
					})
				}
			})
		},
		// 时间格式化
      	dateFormat: function (row, column, cellValue, index){
          	return format(row[column.property])
      	},
		// 状态格式化
      	statusFormat: function (row, column, cellValue, index){
          if(row.status == 1){
            return "草稿"
          }else if(row.status == 2){
            return "已发布"
          }else if(row.status == 3){
            return "已下架"
          }
      	},
	},
	mounted() {
	}
}
</script>

<style scoped>

</style>
